import {
  areaSelectList,
  consumerSelectList,
  imsSelectList,
  lineStationSelectList,
  producerSelectList,
  skuSelectList,
  userSelectList,
  wharfSelectList,
  shelfSelectList,
} from '@/api/select'

const state = {
  areaList: {
    arr: [],
    map: {},
  },
  imsList: {
    arr: [],
    map: {},
  },
  producerList: {
    arr: [],
    map: {},
  },
  skuList: {
    arr: [],
    map: {},
  },
  userList: {
    arr: [],
    map: {},
  },
  wharfList: {
    arr: [],
    map: {},
  },
  consumerList: {
    arr: [],
    map: {},
  },
  lineStationList: {
    arr: [],
    map: {},
  },
  shelfList: {
    arr: [],
    map: {},
  },
}

const mutations = {
  SET_DATA: (state, { key, value, map }) => {
    state[key].arr = value
    state[key].map = map
  },
}

const actions = {
  async fetchData({ commit }) {
    const getData = (data, name = 'name') =>
      data.map((item) => ({
        ...item,
        text: item[name],
        label: item[name],
        value: +item.id,
      }))

    const getMap = (data) =>
      data.reduce((pre, curr) => {
        pre[curr?.value] = curr.text
        return pre
      }, {})
    try {
      const areaList = getData(await areaSelectList(), 'areaCode') || []
      const imsList = getData(await imsSelectList(), 'imsCode') || []
      const producerList =
        getData(await producerSelectList(), 'producerCode') || []
      const skuList = getData(await skuSelectList(), 'skuCode') || []
      const userList = getData(await userSelectList()) || []
      const wharfList = getData(await wharfSelectList(), 'wharfCode') || []
      const consumerList = getData(await consumerSelectList()) || []
      const lineStationList =
        getData(await lineStationSelectList(), 'stationCode') || []
      const shelfList = getData(await shelfSelectList(), 'shelfCode') || []

      commit('SET_DATA', {
        key: 'areaList',
        value: areaList,
        map: getMap(areaList),
      })
      commit('SET_DATA', {
        key: 'imsList',
        value: imsList,
        map: getMap(imsList),
      })
      commit('SET_DATA', {
        key: 'producerList',
        value: producerList,
        map: getMap(producerList),
      })
      commit('SET_DATA', {
        key: 'skuList',
        value: skuList,
        map: getMap(skuList),
      })
      commit('SET_DATA', {
        key: 'userList',
        value: userList,
        map: getMap(userList),
      })
      commit('SET_DATA', {
        key: 'wharfList',
        value: wharfList,
        map: getMap(wharfList),
      })
      commit('SET_DATA', {
        key: 'consumerList',
        value: consumerList,
        map: getMap(consumerList),
      })
      commit('SET_DATA', {
        key: 'lineStationList',
        value: lineStationList,
        map: getMap(lineStationList),
      })
      commit('SET_DATA', {
        key: 'shelfList',
        value: shelfList,
        map: getMap(shelfList),
      })
    } catch (e) {}
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
}
